package assignmentonepenguins



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Listing)
class ListingTests {

    void test_RequiredFields() {
        // Test missing required fields (L-1)
        Customer seller123 = new Customer(email: 'seller123@gmail.com', password: 'abc123')
        Listing goodListing = new Listing()
        assert !goodListing.validate()

        // Test all required fields present EXCEPT seller (L-3)
        goodListing.setName('A Brand New Camera')
        goodListing.setEndDate(new Date() + 7)
        goodListing.setStartBid(149.99)
        assert !goodListing.validate()

        // Test all required fields present/optional fields missing (L-1, L-2)
        goodListing.setSeller(seller123)
        assert goodListing.validate()

        // Test optional field present & description is less than 256 chars (L-2, L-4)
        goodListing.setDescription('This is a fabulous digital camera which is still in the box!')
        assert goodListing.validate()
    }

    void test_DescriptionLengthTooLong() {
        // Test description more than 256 characters (L-4)
        Customer seller123 = new Customer(email: 'seller123@gmail.com', password: 'abc123')
        Listing tooLong = new Listing()
        tooLong.setName('A Brand New Camera')
        tooLong.setEndDate(new Date() + 7)
        tooLong.setStartBid(149.99)
        tooLong.setSeller(seller123)
        tooLong.setDescription('''This is an incredibly long and redundant description...
                                      it is definitely more that 256 characters!!
                                      This is an incredibly long and redundant description...
                                      it is definitely more that 256 characters!!
                                      This is an incredibly long and redundant description...
                                      it is definitely more that 256 characters!!
                                      This is an incredibly long and redundant description...
                                      it is definitely more that 256 characters!!''')
        assert !tooLong.validate()
    }

    void test_EndDateInFuture() {
        // Test end Date/Time must be in the future - endDate in past (L-5)
        Customer seller123 = new Customer(email: 'seller123@gmail.com', password: 'abc123')
        Listing dateListing = new Listing()
        dateListing.setName('A Brand New Camera')
        dateListing.setStartBid(149.99)
        dateListing.setSeller(seller123)
        dateListing.setEndDate(new Date() - 1)
        assert !dateListing.validate()

        // Test end Date/Time must be in the future - endDate in future (L-5)
        dateListing.setEndDate(new Date() + 1)
        assert dateListing.validate()
    }

    void test_NameLength() {
        // Test name must be less than 64 chars - Valid name (L-6)
        Customer seller123 = new Customer(email: 'seller123@gmail.com', password: 'abc123')
        Listing nameListing = new Listing()
        nameListing.setName('A Brand New Camera')
        nameListing.setEndDate(new Date() + 7)
        nameListing.setStartBid(149.99)
        nameListing.setSeller(seller123)
        assert nameListing.validate()

        // Test name must be less than 64 chars - Invalid name (L-6)
        nameListing.setName('''This name is very unhelpful due to the fact that it is
                               really really really really really really really long''')
        assert !nameListing.validate()
    }

    void test_OptionalWinner() {
        // Test nullable winner - Null Winner (L-7)
        Customer seller123 = new Customer(email: 'seller123@gmail.com', password: 'abc123')
        Customer winner123 = new Customer(email: 'winner123@gmail.com', password: '123abc')
        Listing winnerListing = new Listing()
        winnerListing.setName('A Brand New Camera')
        winnerListing.setEndDate(new Date() + 7)
        winnerListing.setStartBid(149.99)
        winnerListing.setSeller(seller123)
        assert winnerListing.validate()

        // Test nullable winner - Non-Null Winner (L-7)
        winnerListing.setWinner(winner123)
        assert winnerListing.validate()
    }

    void test_ListOfBids() {
        // Test a Listing has a list of Bids (B-4)
        Customer seller123 = new Customer(email: 'seller123@gmail.com', password: 'abc123')
        Listing someListing = new Listing()
        someListing.setName('A Brand New Camera')
        someListing.setEndDate(new Date() + 7)
        someListing.setStartBid(149.99)
        someListing.setSeller(seller123)
        someListing.addToBiddings(new Bidding())
        assert someListing.biddings
    }
}
